package kr.hkit.powerjava.alone;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import java.awt.FlowLayout;

import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MiletoKm extends JFrame implements ActionListener {

	private JPanel contentPane;
	private LabelandTextPanel panel;
	private JButton btntrans;
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MiletoKm frame = new MiletoKm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MiletoKm() {
		initialize();
	}

	private void initialize() {
		setTitle("마일을 킬로미터로 변환");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 297, 142);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		panel = new LabelandTextPanel("거리를 마일 단위로 입력하시오");
		contentPane.add(panel);

		btntrans = new JButton("변환");
		btntrans.addActionListener(this);
		btntrans.setHorizontalAlignment(SwingConstants.LEADING);
		contentPane.add(btntrans);

		textField = new JTextField();
		contentPane.add(textField);
		textField.setColumns(20);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("변환")) {
			int mile = 0;
			try {
				mile = Integer.parseInt(panel.getTextField().trim());
			} catch (NumberFormatException err) {
				JOptionPane.showMessageDialog(this, "숫자만 가능");
				panel.setTextField("");
			}
			textField.setText(mile + " 마일은 " + mile * 1.6 + " 킬로미터입니다.");
		}
	}
}
